package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Branches;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

public class BranchDAO extends DAO {

    public BranchDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Branches> getAll() throws Exception {
        List<Branches> a = (List<Branches>) getSession().createQuery("from Branches a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Branches get(int id) throws Exception {
        Branches a = (Branches) getSession().createQuery("from Branches a where a.branchid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Branches a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Branches a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Branches> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Branches> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Branches> findByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        List<Branches> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //Other methods
    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String branchcode) throws Exception {
        Branches e = (Branches) getSession().createQuery("from Branches b where b.branchcode=:code").setParameter("code", branchcode).setCacheable(true).uniqueResult();
        flush();
        if (e == null) {
            return false;
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    public Branches getByBranchCode(String brcode) throws Exception {
        Branches b = (Branches) getSession().createQuery("from Branches b where b.branchcode=:code").setParameter("code", brcode).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return b;
    }
}
